package com.baizhi.controller;

import com.baizhi.entity.Admin;
import com.baizhi.entity.Result;
import com.baizhi.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("login")
    @ResponseBody
    public Result login(String username, String password, String code, HttpServletRequest request) throws UnsupportedEncodingException {
        Result result = new Result();
        String securityCode = (String) request.getSession().getAttribute("securityCode");
        try {
            if(securityCode.equals(code)){
                System.out.println(username+"============="+password);
                Admin admin = adminService.login(username, password);
                if (admin!=null){
                    request.getSession().setAttribute("admin", admin);
                    result.setStatus(true);
                }
            }else {
                throw new RuntimeException("验证码错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}